package com.chinafree.business.service.impl;

import com.chinafree.business.model.result.BaseUserResult;
import com.chinafree.business.model.result.UserBaseMessageResult;
import com.chinafree.business.model.result.UserEducationResult;
import com.chinafree.business.model.result.UserWorkResult;
import com.chinafree.business.service.PDFService;
import com.chinafree.common.PDF.PdfEventHandler;
import com.chinafree.common.utils.TecentCos;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.color.DeviceRgb;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PDFServiceImpl implements PDFService {
    public  PdfFont font = null;
    Color gray = new DeviceRgb(232, 232, 232);
    Color green = new DeviceRgb(33, 125, 91);


    private void create(){
        try {
            byte[] aByte = new TecentCos().getByte("NotoSansCJK-Regular.otf");
            font = PdfFontFactory.createFont(aByte, PdfEncodings.IDENTITY_H, false);
        } catch (IOException e) {
            log.info("字体创建失败：{}", e);
        }
    }

@Override
public void createPDF(String filename, UserBaseMessageResult responseData) throws IOException {
    PdfWriter writer = new PdfWriter(filename);
    create();
    PdfDocument pdf = new PdfDocument(writer);
    PdfEventHandler handler = new PdfEventHandler("hiredFree", 0, 0);
    pdf.addEventHandler(PdfDocumentEvent.START_PAGE, handler);
    Document document = new Document(pdf, PageSize.A4);
    buildDocument(document, responseData);
    document.close();
}

    private void buildDocument(Document document, UserBaseMessageResult responseData) {
        //1. 设置top
//        buildTop(document);


        BaseUserResult baseUserResult = responseData.getBaseUserResult();
        buildBaseUser(document, baseUserResult);


        String summary = responseData.getBaseUserResult().getSummary();
        buildIntroduction(document, summary);

        List<UserWorkResult> userWorkResult = responseData.getUserWorkResult();
        buildWorkExperience(document, userWorkResult);

        List<UserEducationResult> userEducationResult = responseData.getUserEducationResult();
        buildEducationExperience(document, userEducationResult);

    }

private void buildEducationExperience(Document document, List<UserEducationResult> userEducationResults) {
    Table table = new Table(new float[]{510});
    table.setHorizontalAlignment(HorizontalAlignment.CENTER);
    table.setBorder(Border.NO_BORDER);
    table.setPadding(1);
    table.addCell(buildCell("教育经历", 9, gray));
    for (UserEducationResult userEducationResult : userEducationResults) {
        Cell cell1 = new Cell();
        cell1.setBorder(Border.NO_BORDER);
        com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List();
        list.add(getListItem(userEducationResult.getSchool(), 11));
        list.add(getListItem(userEducationResult.getDegreeName()==null?"":userEducationResult.getDegreeName(), 8));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY年MM月");
        list.add(getListItem(formatter.format(userEducationResult.getStartTime()) + "-" + formatter.format(userEducationResult.getEndTime()), 8));
        list.add(getListItem(userEducationResult.getDescription(), 9));
        list.setListSymbol("");
        cell1.add(list);
        table.addCell(cell1);
    }
    document.add(table);
}
private void buildWorkExperience(Document document, List<UserWorkResult> userWorkResults) {
    Table table = new Table(new float[]{510});
    table.setHorizontalAlignment(HorizontalAlignment.CENTER);
    table.setBorder(Border.NO_BORDER);
    table.setPadding(1);
    table.addCell(buildCell("工作经历", 9, gray));
    for (UserWorkResult userWorkResult : userWorkResults) {
        table.addCell(buildCell(userWorkResult.getCompany(), 11));
        table.addCell(buildCell(userWorkResult.getPosition(), 8));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY年MM月");
        table.addCell(buildCell(formatter.format(userWorkResult.getStartTime()) + "-" + formatter.format(userWorkResult.getEndTime()), 8));
        table.addCell(buildCell(userWorkResult.getDescription(), 10));
        table.addCell(new Cell().setBorder(null));
    }
    document.add(table);
}
private void buildIntroduction(Document document, String summary) {
    Table table = new Table(new float[]{510});
    table.setHorizontalAlignment(HorizontalAlignment.CENTER);
    table.setBorder(Border.NO_BORDER);
    table.setPadding(1);
    table.addCell(buildCell("简介", 9, gray));
    table.addCell(buildCell(summary, 8));
    document.add(table);
}
private Cell buildCell(String msg, Integer size) {
    if (msg == null) {
        return new Cell().setBorder(null);
    }
    Cell cell = new Cell();
    cell.add(msg);
    cell.setFont(font);
    cell.setFontSize(size);
    cell.setBorder(Border.NO_BORDER);
    return cell;
}
private Cell buildCell(String msg, Integer size, Color color) {
    Cell cell = new Cell();
    cell.add(msg);
    cell.setHeight(10);
    cell.setFont(font);
    cell.setFontSize(size);
    cell.setBorder(Border.NO_BORDER);
    cell.setBackgroundColor(gray);
    cell.setFontColor(green);
        cell.setMarginTop(10);
        return cell;
    }

private void buildBaseUser(Document document, BaseUserResult baseUserResult) {
    Table table = new Table(new float[]{100, 410});
    table.setBorder(null);
    table.setHorizontalAlignment(HorizontalAlignment.CENTER);
    table.setMargin(10);
    //1.
    Cell cell = new Cell();
    try {
        byte[] aByte = new TecentCos().getByte("yyqx.jpg");
        ImageData image = ImageDataFactory.create(aByte);
        Image image1 = new Image(image);
        cell.setPadding(8);
        image1.scaleToFit(84, 84);
        cell.add(image1);
        cell.setBorder(Border.NO_BORDER);
        cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.addCell(cell);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //2.
    Cell cell1 = new Cell();
    cell1.setPadding(8);
    cell1.setBorder(Border.NO_BORDER);
    long age = baseUserResult.getBorn().until(LocalDateTime.now(), ChronoUnit.YEARS);
    com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List();
    list.add(getListItem(baseUserResult.getFirstname() + " " + baseUserResult.getLastname(), 11));
    list.add(getListItem(baseUserResult.getLastname(), 8));
    list.add(getListItem(age + "岁" + "|" + baseUserResult.getJobTypeName(), 8));
    list.add(getListItem("住在" + baseUserResult.getLiveCityName(), 8));
    list.add(getListItem("Tel：" + baseUserResult.getPhone() + "          wechat：" + baseUserResult.getWechat(), 8));
    list.setListSymbol("");
    cell1.add(list);
    table.addCell(cell1);
    document.add(table);
}

    private ListItem getListItem(String text, int size) {
        ListItem item = new ListItem(text);
        item.setFontSize(size);
        item.setFont(font);
        return item;
    }

    @Override
    public void buildTop(Document document) {

        Table table = new Table(new float[]{250, 270});
        table.setBorderBottom(new SolidBorder(gray, 1));
        //1.
        Cell cell = new Cell();
        cell.add("hiredFree");
        cell.setFontSize(14);
        cell.setFontColor(green);
        cell.setBorder(Border.NO_BORDER);
        cell.setHorizontalAlignment(HorizontalAlignment.LEFT);
        table.addCell(cell);


        //2.time
        Cell cell1 = new Cell();
        cell1.add(new Date().toString());
        cell1.setFontSize(8);
        cell1.setFontColor(Color.BLACK);
        cell1.setBorder(Border.NO_BORDER);
        cell1.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        cell1.setVerticalAlignment(VerticalAlignment.BOTTOM);
        table.addCell(cell1);
        document.add(table);
    }
}







